package com.szgr.baoyin.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.szgr.baoyin.R;
import com.szgr.baoyin.model.OrderInfo;
import com.szgr.baoyin.myview.NoScrollListView;
import com.szgr.baoyin.utils.DateUtils;
import com.szgr.baoyin.utils.MyTool;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by yang on 15/12/5.
 */
public class MyOrderListAdapter extends BaseAdapter {
    private Context mContext;
    private List<OrderInfo> data;
    private View.OnClickListener listener;
    public MyOrderListAdapter(Context context,List<OrderInfo> data,View.OnClickListener listener){
        mContext = context;
        this.data = data;
        this.listener = listener;
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_my_order_list_view,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        OrderInfo orderInfo = data.get(position);
        List<OrderInfo.MyOrderItem> order_list = orderInfo.getOrder_list();
        OrderInfo.MyOrderItem myOrderItem = order_list.get(0);
        String goods_is_service = myOrderItem.getGoods_is_service();
        if(!"01".equals(goods_is_service)) {
            List<OrderInfo.MyOrderItem.ExtendOrderGoods> extend_order_goods = myOrderItem.getExtend_order_goods();
            holder.nslv_order.setAdapter(new OrderItemNoScrollAdapter(mContext, extend_order_goods));

            int num = 0;
            for (OrderInfo.MyOrderItem.ExtendOrderGoods eg : extend_order_goods) {
                num += Integer.parseInt(eg.getGoods_num());
            }
            holder.tv_num.setText("共" + num + "件商品");
            holder.nslv_order.setVisibility(View.VISIBLE);
        }else{
            holder.nslv_order.setVisibility(View.GONE);
            holder.tv_num.setText("共1件商品");
        }

        holder.tv_orderNum.setText("订单号：" + myOrderItem.getOrder_sn());
        holder.tv_orderNum.setTag(position);
        holder.tv_orderNum.setOnClickListener(listener);

        holder.tv_price.setText("¥" + myOrderItem.getOrder_amount() + "元");

        String state = myOrderItem.getOrder_state();
        holder.tv_btn.setTag(position);
        holder.tv_connect.setTag(position);
        holder.tv_cancel.setTag(position);
        switch (state){
            case "10":
                holder.tv_btn.setText("去支付");
                holder.tv_btn.setVisibility(View.VISIBLE);
                holder.tv_connect.setVisibility(View.VISIBLE);
                holder.tv_cancel.setVisibility(View.VISIBLE);
                break;
            case "20":
                holder.tv_btn.setVisibility(View.GONE);
                holder.tv_connect.setVisibility(View.GONE);
                holder.tv_cancel.setVisibility(View.GONE);
                break;
            case "30":
                holder.tv_btn.setText("确认收货");
                holder.tv_btn.setVisibility(View.VISIBLE);
                holder.tv_connect.setVisibility(View.GONE);
                holder.tv_cancel.setVisibility(View.GONE);
                break;
            case "40":
                holder.tv_btn.setText("去评价");
                holder.tv_btn.setVisibility(View.VISIBLE);
                holder.tv_connect.setVisibility(View.GONE);
                holder.tv_cancel.setVisibility(View.GONE);
                break;
        }
        holder.tv_btn.setOnClickListener(listener);
        holder.tv_connect.setOnClickListener(listener);
        holder.tv_cancel.setOnClickListener(listener);

        String add_time = myOrderItem.getAdd_time();
        String s = DateUtils.paseDate1(Long.parseLong(add_time));
        holder.tv_time.setText("下单时间：" + s);
        return convertView;
    }

    public void setData(List<OrderInfo> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    class ViewHolder {
        NoScrollListView nslv_order;
        TextView tv_num;
        TextView tv_price;
        TextView tv_btn;
        TextView tv_orderNum;
        TextView tv_time;
        TextView tv_connect;
        TextView tv_cancel;

        public ViewHolder(View view){
            nslv_order = (NoScrollListView) view.findViewById(R.id.order_no_scroll_list_view);
            tv_num = (TextView) view.findViewById(R.id.order_goods_num);
            tv_orderNum = (TextView) view.findViewById(R.id.order_number);
            tv_price = (TextView) view.findViewById(R.id.order_goods_price);
            tv_btn = (TextView) view.findViewById(R.id.order_goods_btn);
            tv_time = (TextView) view.findViewById(R.id.add_time);
            tv_connect = (TextView) view.findViewById(R.id.order_goods_btn2);
            tv_cancel = (TextView) view.findViewById(R.id.order_goods_btn1);
        }
    }
}
